package com.hhkj.admin.train.factroy;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.train.domain.Train;
import com.hhkj.admin.train.domain.TrainStudentAnswer;
import com.hhkj.admin.train.model.req.TrainReq;
import com.hhkj.admin.train.service.base.TrainStudentAnswerService;
import com.ruoyi.common.utils.spring.SpringUtils;

import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.stream.Collectors;

/**
 * 异步存储提交答案工厂
 *
 * @author ruoyi
 */
public class AsyncTrainAnswerFactory
{

    /**
     * 操作日志记录
     *
     * @param studentAnswers 操作日志信息
     * @return 任务task
     */
    public static TimerTask asyncSaveStudentAnswer(TrainReq trainReq, List<TrainStudentAnswer> studentAnswers)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                for (TrainStudentAnswer studentAnswer : studentAnswers) {
                    studentAnswer.setStudentId(trainReq.getStudentId());
                    studentAnswer.setTrainId(trainReq.getId());
                    studentAnswer.setCreateBy(trainReq.getCreateBy());
                    studentAnswer.setCreateTime(trainReq.getCreateTime());
                }
                // 保存信息
                SpringUtils.getBean(TrainStudentAnswerService.class).saveBatch(studentAnswers);
            }
        };
    }

    /**
     * 操作日志记录
     *
     * @param trainReq 培训信息
     * @return 任务task
     */
    public static TimerTask asyncUpdateStudentAnswer(TrainReq trainReq)
    {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                TrainStudentAnswerService trainStudentAnswerService
                        = SpringUtils.getBean(TrainStudentAnswerService.class);

                LambdaQueryWrapper<TrainStudentAnswer> trainQuery = new LambdaQueryWrapper<>();
                trainQuery.eq(TrainStudentAnswer::getTrainId, trainReq.getId());

                List<TrainStudentAnswer> studentAnswerList
                        = trainStudentAnswerService.list(trainQuery);

                List<TrainStudentAnswer> answerList = trainReq.getAnswerList();
                Map<Long, TrainStudentAnswer> answerMapByQuestionId = answerList.stream()
                        .collect(Collectors.groupingBy(TrainStudentAnswer::getQuestionId,
                                Collectors.collectingAndThen(Collectors.toList(),
                                        CollectionUtil::getFirst)));

                TrainStudentAnswer newAnswer;
                for (TrainStudentAnswer trainStudentAnswer : studentAnswerList) {
                    if (answerMapByQuestionId.containsKey(trainStudentAnswer.getQuestionId())) {
                        newAnswer
                                = answerMapByQuestionId.get(trainStudentAnswer.getQuestionId());
                        trainStudentAnswer.setStudentAnswer(newAnswer.getStudentAnswer());
                        trainStudentAnswer.setCorrectResCode(newAnswer.getCorrectResCode());
                        trainStudentAnswer.setQuestionMark(newAnswer.getQuestionMark());
                        trainStudentAnswer.setStudentMark(newAnswer.getStudentMark());
                        trainStudentAnswer.setUpdateBy(trainReq.getUpdateBy());
                        trainStudentAnswer.setDisableUpdateByFill(true);
                    }
                }
                // 保存信息
                trainStudentAnswerService.updateBatchById(studentAnswerList);
            }
        };
    }

}
